package blockr.function;

/**
 * @author spencercjh
 */
public class Fill_Final_Piece {
    public static void Fill(byte[][] All_Piece, byte[][][] All_Block, int arraySize) {
        long start_fill = System.currentTimeMillis();
        byte[] IC_CODE = new byte[4];
        IC_CODE[0] = 0;
        IC_CODE[1] = 15;  //补码00001111
        IC_CODE[2] = -1;  //补码11111111
        IC_CODE[3] = -16; //补码11110000
        if (MappedBiggerFileReader.isAdd()) {    //添加新片
            byte[][] end_block;
            byte[][] final_block;
            byte[] old_final_piece;
            byte[] new_final_piece;
            if (MappedBiggerFileReader.getPiece_index() % (arraySize * 8) == 0) {  //总片数正好是1024的整倍数，新加片和原最后一片不在同一块上
                final_block = All_Block[MappedBiggerFileReader.getBlock_index() - 1];
                old_final_piece = final_block[arraySize * 8 - 1];
                if ((MappedBiggerFileReader.getPiece_index() % (arraySize * 8)) - 1 >= 0) {    //原最后一片还需要填充标识码
                    int i = 0, length = 1;
                    while (old_final_piece[i++] != 0) {
                        length++;
                    }
                    int fill_length = arraySize - length;
                    new_final_piece = new byte[arraySize];
                    System.arraycopy(old_final_piece, 0, new_final_piece, 0, length);
                    System.arraycopy(IC_CODE, 1, new_final_piece, i, fill_length);
                } else {  //原最后一片已满，不需要填充标识码
                    end_block = All_Block[MappedBiggerFileReader.getBlock_index()];
                    new_final_piece = end_block[0];
                    System.arraycopy(IC_CODE, 1, new_final_piece, 0, 3);
                    end_block[0] = new_final_piece;
                    All_Block[MappedBiggerFileReader.getBlock_index()] = end_block;
                }
            } else {
                final_block = All_Block[MappedBiggerFileReader.getBlock_index()];
                old_final_piece = final_block[(MappedBiggerFileReader.getPiece_index() % (arraySize * 8)) - 1];   //n-1
//            byte[] old_final_piece = All_Piece[All_Piece.length - 2];
                new_final_piece = new byte[arraySize];
                int index = old_final_piece.length;
                System.arraycopy(old_final_piece, 0, new_final_piece, 0, index);
                int j = 1;
                for (int i = index; i < arraySize; i++, j++) {   //填充ic_code
                    new_final_piece[i] = IC_CODE[j];
                }
//                j--;    //最后多一个j++
                final_block[(MappedBiggerFileReader.getPiece_index() % (arraySize * 8)) - 1] = new_final_piece;
                All_Piece[MappedBiggerFileReader.getPiece_index() - 1] = new_final_piece;
//            byte[] end_piece = All_Piece[All_Piece.length - 1];
                byte[] end_piece = final_block[(MappedBiggerFileReader.getPiece_index()) % (arraySize * 8)]; //n
                for (int i = j, k = 0; i < IC_CODE.length; i++, k++) { //继续填充ic_code
                    end_piece[k] = IC_CODE[i];
                }
                int ic_code_length = 0;
                for (int i = 0; end_piece[i] != 0; i++) {
                    ic_code_length++;
                }
                int random_number_length = arraySize - ic_code_length;
//                ic_code_length++;   //从ic_code_length后一位开始填充随机数
                byte[] random_num = RandomNum.generate_random_num(random_number_length);
                for (int i = ic_code_length; i < arraySize; i++) {    //填充随机数
                    end_piece[i] = random_num[j];
                }
                final_block[(MappedBiggerFileReader.getPiece_index()) % (arraySize * 8)] = end_piece;
                All_Piece[MappedBiggerFileReader.getPiece_index()] = end_piece;
                All_Block[MappedBiggerFileReader.getBlock_index()] = final_block;
            }
        } else {  //不添加新片
            byte[][] final_block = All_Block[MappedBiggerFileReader.getBlock_index()];
            byte[] old_final_piece = final_block[(MappedBiggerFileReader.getPiece_index() % (arraySize * 8)) - 1];
//            byte[] old_final_piece = All_Piece[All_Piece.length - 1];
            int index = old_final_piece.length;
//            byte[] new_final_piece=old_final_piece.clone();
            byte[] new_final_piece = new byte[arraySize];
            System.arraycopy(old_final_piece, 0, new_final_piece, 0, index);
            new_final_piece[index] = IC_CODE[1];    //填充24位标识码 3个byte
            new_final_piece[index + 1] = IC_CODE[2];
            new_final_piece[index + 2] = IC_CODE[3];
            index += 3;
            int random_number_length = arraySize - index; //随机数长度
//            index++;    //随机数从后面一位开始填充
            byte[] random_num = RandomNum.generate_random_num(random_number_length);
            for (int i = index, j = 0; i < arraySize; i++, j++) { //填充随机数
                new_final_piece[i] = random_num[j];
            }
            final_block[(MappedBiggerFileReader.getPiece_index() % (arraySize * 8)) - 1] = new_final_piece;
            All_Piece[MappedBiggerFileReader.getPiece_index() - 1] = new_final_piece;
            All_Block[MappedBiggerFileReader.getBlock_index()] = final_block;
        }

        long end_fill = System.currentTimeMillis();
        System.out.println("填充最后片耗时：" + String.valueOf(end_fill - start_fill));
    }

}